﻿using AIStudio.Wpf.DiagramDesigner.Geometrys;

namespace AIStudio.Wpf.DiagramDesigner
{
    public class PointHelper
    {
        public static PointBase GetPointForConnector(FullyCreatedConnectorInfo connector)
        {
            PointBase point = new PointBase();
            if (connector == null)
            {
                return point;
            }

            if (connector.IsInnerPoint)
            {
                point = new PointBase(connector.DataItem.Left + connector.DataItem.ItemWidth * connector.XRatio - connector.ConnectorWidth / 2,
                        connector.DataItem.Top + connector.DataItem.ItemHeight * connector.YRatio - connector.ConnectorHeight / 2);
            }
            else if (connector.IsPortless)
            {
                point = connector.DataItem?.GetBounds().Center?? connector.MiddlePosition;
            }
            else
            {
                switch (connector.Orientation)
                {
                    case ConnectorOrientation.Left:
                        point = new PointBase(connector.DataItem.Left - connector.ConnectorWidth / 2, connector.DataItem.Top + (connector.DataItem.ItemHeight / 2) - connector.ConnectorHeight / 2);
                        break;
                    case ConnectorOrientation.TopLeft:
                        point = new PointBase(connector.DataItem.Left - connector.ConnectorWidth / 2, connector.DataItem.Top - connector.ConnectorHeight / 2);
                        break;
                    case ConnectorOrientation.Top:
                        point = new PointBase(connector.DataItem.Left + (connector.DataItem.ItemWidth / 2) - connector.ConnectorWidth / 2, connector.DataItem.Top - connector.ConnectorHeight / 2);
                        break;
                    case ConnectorOrientation.TopRight:
                        point = new PointBase(connector.DataItem.Left + connector.DataItem.ItemWidth - connector.ConnectorWidth / 2, connector.DataItem.Top - connector.ConnectorHeight / 2);
                        break;
                    case ConnectorOrientation.Right:
                        point = new PointBase(connector.DataItem.Left + connector.DataItem.ItemWidth - connector.ConnectorWidth / 2, connector.DataItem.Top + (connector.DataItem.ItemHeight / 2) - connector.ConnectorHeight / 2);
                        break;
                    case ConnectorOrientation.BottomRight:
                        point = new PointBase(connector.DataItem.Left + connector.DataItem.ItemWidth - connector.ConnectorWidth / 2, connector.DataItem.Top + connector.DataItem.ItemHeight - connector.ConnectorHeight / 2);
                        break;
                    case ConnectorOrientation.Bottom:
                        point = new PointBase(connector.DataItem.Left + (connector.DataItem.ItemWidth / 2) - connector.ConnectorWidth / 2, connector.DataItem.Top + connector.DataItem.ItemHeight - connector.ConnectorHeight / 2);
                        break;
                    case ConnectorOrientation.BottomLeft:
                        point = new PointBase(connector.DataItem.Left - connector.ConnectorWidth / 2, connector.DataItem.Top + connector.DataItem.ItemHeight - connector.ConnectorHeight / 2);
                        break;
                    default:
                        point = new PointBase(connector.DataItem.Left + (connector.DataItem.ItemWidth / 2) - connector.ConnectorWidth / 2, connector.DataItem.Top + (connector.DataItem.ItemHeight / 2) - connector.ConnectorHeight / 2);
                        break;
                }
            }
            return point;
        }


    }
}
